#include <utillib/util/userfunctionmanager.h>

using namespace utillib::util;


UserFunctionManager::UserFunctionManager()
{
}

UserFunctionManager::~UserFunctionManager()
{
	m_functionNameList.removeAllMemory();
	m_functionList.clear();
}

void UserFunctionManager::addUserFunction( char *name, UserFunction func )
{
	FunctionProperty *functionProperty = new FunctionProperty;
	strncpy( functionProperty->name, name, FUNCTION_NAME_LENGTH );
	functionProperty->function = func;
	m_functionNameList.insert( functionProperty->name, functionProperty );
}

void UserFunctionManager::matchUserFunction( char *name, void *key )
{
	FunctionProperty *functionProperty = m_functionNameList.find( name );
	if ( functionProperty )
	{
		if ( functionProperty->function ) m_functionList.insert( mapFunction::value_type( key, functionProperty->function ) );
	}
}

UserFunctionManager::UserFunction UserFunctionManager::getUserFunction( void *key )
{
	mapFunction::iterator iter = m_functionList.find( key );

	if ( iter != m_functionList.end() )
	{
		UserFunction function = ( *iter ).second;
		return ( function );
	} 
	return ( NULL );
}

bool UserFunctionManager::insertUserFunction( void *key, UserFunction func )
{
	if ( m_functionList.find( key ) == m_functionList.end() ) return ( false );

	m_functionList.insert( mapFunction::value_type( key, func ) );

	return ( true );
}
